<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
require_once(dirname(__FILE__)."/../../vendor/autoload.php");
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Up extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	public function up_pic() {
		$path = isset($_POST['path'])?$_POST['path']:'product/pic';
		$file=$_FILES['Filedata'];
		$tempFile = $file['tmp_name'];
		$fileParts = pathinfo($file['name']);
		$filename = date("YmdHis") . '_' . rand(10000, 99999).'.'.$fileParts['extension'];
		if(!empty($path))
		{
			$filename = "$path/$filename";
		}
		$fileTypes = array('jpg','jpeg','gif','png','bmp'); // File extensions
		if (in_array($fileParts['extension'],$fileTypes)) {
			$accessKey = 'if9TbJKjKfmJfcfqsy6MmhuDDLetDmwgcAHcj3Ib';
			$secretKey = 'YOESNYsRTVxf8Hp0UTOVPzfk1WvMg6UWXiQ1agiB';
			$auth = new Auth($accessKey, $secretKey);
			
			$bucket = 'jishibao';
			// 设置put policy的其他参数
			$opts = array(
			            'callbackBody' => 'name=$(fname)&hash=$(etag)'
			        );
			$token = $auth->uploadToken($bucket, null, 3600, $opts);
			$uploadMgr = New UploadManager();
			
			list($ret, $err) = $uploadMgr->putFile($token, $filename, $tempFile,$opts);
			//echo "\n====> putFile result: \n";
			if ($err !== null) {
			    var_dump($err);
			} else {
			    echo $ret['key'];
			}
		}else{
			echo "error";
		}
		die();
	
	}
	
}